package top.ffhome.multithread.ch12.demo02.activeobject;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ActiveObjectImpl implements ActiveObject {
    private final ExecutorService service = Executors.newSingleThreadExecutor();

    @Override
    public Future<String> makeString(int count, char fillChar) {
        return (Future<String>) service.submit(() -> {
            char[] buffer = new char[count];
            for (int i = 0; i < count; i++) {
                buffer[i] = fillChar;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
            }
            return new String(buffer);
        });
    }

    @Override
    public void displayString(String string) {
        service.execute(() -> {
            try {
                System.out.println("displayString: " + string);
                Thread.sleep(10);
            } catch (InterruptedException e) {
            }
        });
    }

    @Override
    public void shutdown() {
        service.shutdown();
    }
}
